package com.googlecode.bip.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.xml.sax.SAXException;
import com.googlecode.bip.entity.IllegalAttributeException;
import com.googlecode.bip.xml.importe.ImportXMLSurveyFactory;

/**
 * {@link com.googlecode.bip.servlet.FileUploadHttpServlet}
 */
public class FileUploadHttpServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    @Override
    protected void doPost(
            HttpServletRequest  request,
            HttpServletResponse response
            )
    throws ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        out.println("<h1>File Upload</h1>");
        out.println();

        boolean isMultipart = ServletFileUpload.isMultipartContent(request);

        if (isMultipart) {
            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload();

            // Parse the request
            try {
                FileItemIterator iter = upload.getItemIterator(request);
                while (iter.hasNext()) {
                    FileItemStream  item    = iter.next();
                    //String          name    = item.getFieldName();
                    InputStream     stream  = item.openStream();

                    if (item.isFormField()) {
                        //System.out.println("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
                        }
                    else {
                        //System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
                        // Process the input stream
                        ImportXMLSurveyFactory.importXML(stream);
                        }
                    }
                }
            catch( FileUploadException e ) {
                throw new ServletException( e );
                }
            catch( ParserConfigurationException e ) {
                throw new ServletException( e );
                }
            catch( SAXException e ) {
                throw new ServletException( e );
                }
            catch( NamingException e ) {
                throw new ServletException( e );
                }
            catch( SQLException e ) {
                throw new ServletException( e );
                }
            catch( IllegalAttributeException e ) {
                throw new ServletException( e );
                }
        }

       /* if( isMultipart ) {
            // Create a factory for disk-based file items
            FileItemFactory factory = new DiskFileItemFactory();

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload(factory);

            try {
                List<?>     items = upload.parseRequest(request);
                Iterator<?> iter  = items.iterator();

                while( iter.hasNext() ) {
                    FileItem item = (FileItem) iter.next();

                    ImportXMLSurveyFactory.importXML(
                            item.getInputStream()
                            );

                    item.delete();
                    }
                }
            catch( FileUploadException e ) {
                throw new ServletException( e );
                }
            catch( ParserConfigurationException e ) {
                throw new ServletException( e );
                }
            catch( SAXException e ) {
                throw new ServletException( e );
                }
            catch( NamingException e ) {
                throw new ServletException( e );
                }
            catch( SQLException e ) {
                throw new ServletException( e );
                }
            }*/

        out.println("<div>done</div>");
        out.println();
    }
}
